package misc;

import java.io.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Logger {
    public enum LogType {error,warning,verbose,debug};

    /*
    * Where is the log file?
    * If you run from Eclipse j2ee, 
    *  - the main directory of the program 
    * If you run on separate Tomcat on Windows,
    *  - C:\Users\YOURNAME\AppData\Local\VirtualStore\Program Files\
    *       Apache Software Foundation\Tomcat 6.0\server.log
    * If you run on separate Tomcat on Linux,
    * - /local/apps/apache-tomcat-7.0.19/bin
    */

    /*
    Write any reports forwarded to a server text file instead of displaying
    details which, could be abused, to users
    */
    public static void write(String message,LogType type)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String log = dateFormat.format(new Date())+" - ";

        switch(type)
        {
            case error:log+="ERROR: "; break;
            case warning:log+="Warning: "; break;
            case verbose:log+="Info: "; break;
            case debug:log+="Debug: "; break;
        }

        log+=message+"\n";

        try{
            FileWriter fstream = new FileWriter(Configurations.LogFile,true);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(log);
            out.close();

            System.out.println("Log is written to the file.");
        }catch (Exception e){
            System.err.println("Error: Encounter an error when trying to "+
            "write to log file - "+e.getMessage());
        }
    }
}
